package org.util;

import java.io.File;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
//import org.apache.commons.mail.EmailException;
//import org.apache.commons.mail.HtmlEmail;

/**
 *
 * @author George.Mavchun
 */
public class Util {

    /**
     * 
     * @param str - string to generate md5 hash.
     * @return md5 hash of input string.
     */
    public static String getMD5Hash(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] md5hash = md.digest(str.getBytes());
            StringBuffer hashString = new StringBuffer(md5hash.length * 2);
            for (byte aMd5hash : md5hash) {
                if ((aMd5hash & 0xff) < 0x10) {
                    hashString.append("0");
                }
                hashString.append(Long.toString(aMd5hash & 0xff, 16));
            }
            return hashString.toString();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

    public static String replaceSpecialChars(String string) {
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == '"') {
                string = string.substring(0, i) + "&quot;" + string.substring(i + 1);
                i = i + 4;
            } else if (string.charAt(i) == '<') {
                string = string.substring(0, i) + "&lt;" + string.substring(i + 1);
                i = i + 3;
            } else if (string.charAt(i) == '>') {
                string = string.substring(0, i) + "&gt;" + string.substring(i + 1);
                i = i + 3;
            }
        }
        return string;
    }

    public static void makeUserDir(String email, String appPath) {
        byte[] bytePath = appPath.getBytes();
        int length = bytePath.length;
        int j = 0;
        int count = 0;

        for (int i = length - 1; i >= 0; i--) {
            if (bytePath[i] == '\\') {
                if (count == 2) {
                    j = i + 1;
                    i = -1;
                }
                count++;
            }
        }

        byte[] newByte = new byte[j];
        for (int i = 0; i < j; i++) {
            newByte[i] += bytePath[i];
        }
        appPath = new String(newByte);
        File userDirectory = new File(appPath + "userFolders/" + email);
        userDirectory.mkdir();
    }
    private static final String[] syllables = {"ba", "be", "bi", "bu", "ca", "ci", "ce", "du", "di", "do", "da", "fa", "fi", "fu", "gu", "ha", "he", "tu", "ta", "ti", "za", "zu"};

    public static String makeNewPass() {
        Random rnd = new Random();
        return syllables[rnd.nextInt(syllables.length)] + syllables[rnd.nextInt(syllables.length)] + syllables[rnd.nextInt(syllables.length)];
    }

    /**
     * 
     * TODO: Find some workable mail sending method.
     * 
     * @param mail
     * @param name
     * @param surname
     * @param newpass
     * @return true if new password sent to user
     */
//    public static boolean sendNewPass(String mail, String name, String surname, String newpass) {
//        try {
//            // Create the email message
//            HtmlEmail email = new HtmlEmail();
//            email.setHostName("yandex.ru");
//            email.addTo(mail, name + " " + surname);
//            email.setFrom("osdiary@yandex.ru", "OSDiary Support");
//            email.setSubject("Test email with inline image");
//
//            email.setAuthentication("osdiary", "osd12345");
//
//            // set the html message
//            email.setHtmlMsg("<html>Password: " + newpass + "</html>");
//
//            // set the alternative message
//            email.setTextMsg("Password: " + newpass);
//
//            // send the email
//            email.send();
//            return true;
//        } catch (EmailException ex) {
//            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
//            return false;
//        }
//    }
}